import {defineConfig} from "vite"
import vue from "@vitejs/plugin-vue"

// ElementPlus组件按需导入（包含其图标）
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'

// 自定义图标组件按需导入
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import {FileSystemIconLoader} from "unplugin-icons/loaders"

// 配置绝对路径别名@
import path from 'path'

const host = process.env.TAURI_DEV_HOST;

// 参考: element-plus-best-practices
export default defineConfig(async () => ({
  resolve: {
    alias: {
      // 配置绝对路径别名@
      '@': path.resolve(__dirname, 'src'),
    },
  },

  plugins: [
    vue(),
    AutoImport({
      imports: ['vue'], // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          prefix: 'he',
          customCollections: ['icon']
        })],
    }),

    Icons({
      autoInstall: false,
      customCollections: {
        'icon': FileSystemIconLoader('src/assets/icons'),
      }
    })
  ],

  css: {
    preprocessorOptions: {
      scss: {
        // The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0
        api: 'modern'
      }
    }
  },

  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    host: host || false,
    hmr: host
      ? {
        protocol: "ws",
        host,
        port: 1421,
      }
      : undefined,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
  },
}));
